<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/5/28
 * Time: 17:25
 * Desc: 根据配方进行生产、进货
 */
namespace app\kuguan\controller;
use controller\BasicAdmin;
use service\DataService;
use think\Db;
use app\kuguan\model\MaterialModel;
use app\kuguan\model\GoodsMaterialModel;
use app\kuguan\model\MaterialStockModel;

class Materialpeifang extends BasicAdmin{
    /**
     * @Author  lingyun
     * @Desc    根据配方使用原材料
     * 2018-05-28
     */
    public function create_jinhuo_peifang_order(){
        $request = request();
        if($request->isPost()){
            $post = input('post.');

            $order_sn = create_order_sn(session('user.id'));
            $id = db('material_order')->insertGetId(['admin_id'=>session('user.id'),'order_sn'=>$order_sn,'status'=>0,'addtime'=>time(),'type'=>1,'order_type'=>2,'yw_type'=>$post['yw_type'],'gys'=>$post['gys']]);       //订货表单
            $res = $this->create_material_arr($post,$id,1);          //创建原料申请订单
            db('material_order')->where('id',$id)->update(['price'=>$res['chengben_price']]);
            db('goods_material_peifang_order')->insertAll($res['data1']);       //插入配方订单记录
            db('material_stock_log')->insertAll($res['data']);          //插入库存记录
            $id>0?$this->success('保存成功',''):$this->error('保存失败，请稍后再试','');
//            $arr = $this->create_arr($post,$id);        //入库数据
//            db('material_stock_log')->insertAll($arr);
//            $id>0?$this->success('保存成功',''):$this->error('保存失败','');
        }else{
            $peifang = db('goods_peifang')->where('is_deleted',0)->order('id desc')->select();      //原材料

            $this->assign('peifang',$peifang);
            $this->assign('title','原料进货');
//            $this->assign('type',input('type'));
//            if(input('type') == 1){         //购货
//                $this->assign('title','原料购货');
//            }else{                          //生产
//                $this->assign('title','生产使用');
//            }
            return $this->fetch();
        }
    }

    /**
     * @Author  lingyun
     * @Desc    根据商品配方进行生产
     * 2018-05-28
     */
    public function create_shengchan_peifang_order(){
        $request = request();
        if($request->isPost()){
            $post = input('post.');
            $order_sn = create_order_sn(session('user.id'));
            $id = db('material_order')->insertGetId(['admin_id'=>session('user.id'),'order_sn'=>$order_sn,'status'=>0,'addtime'=>time(),'receive_time'=>time(),'type'=>2,'order_type'=>2,'yw_type'=>$post['yw_type'],'gys'=>$post['gys'],'receive_admin'=>session('user.id'),'is_duizhang'=>1]);       //订货表单
            $res = $this->create_material_arr($post,$id,2);          //创建原料申请订单
            db('material_order')->where('id',$id)->update(['price'=>$res['chengben_price'],'receive_price'=>$res['chengben_price']]);
            db('goods_material_peifang_order')->insertAll($res['data1']);       //插入配方订单记录
            db('material_stock_log')->insertAll($res['data']);          //插入库存记录
            $id>0?$this->success('保存成功',''):$this->error('保存失败，请稍后再试','');
//            $arr = $this->create_arr($post,$id);        //入库数据
//            db('material_stock_log')->insertAll($arr);
//            $id>0?$this->success('保存成功',''):$this->error('保存失败','');
        }else{
            $peifang = db('goods_peifang')->where('is_deleted',0)->order('id desc')->select();      //原材料

            $this->assign('peifang',$peifang);
            $this->assign('title','原料进货');
//            $this->assign('type',input('type'));
//            if(input('type') == 1){         //购货
//                $this->assign('title','原料购货');
//            }else{                          //生产
//                $this->assign('title','生产使用');
//            }
            return $this->fetch();
        }
    }

    /**
     * @Author  lingyun
     * @Desc    生成原料入库数据
     * 2018-05-28
     */
    public function create_material_arr($post,$order_id,$type){
        $data = [];
        $data1 = [];            //配方订单详情
        $chengben_price = 0;            //订单总价
        foreach($post['id'] as $k => $v){           //循环创建原料入库信息
            $data1[$k]['order_id'] = $order_id;
            $data1[$k]['peifang'] = $v;
            $data1[$k]['addtime'] = time();
            $res = db('goods_peifang_yongliao')->alias('a')->join('ys_goods_material b','a.m_id=b.id','LEFT')->where('a.p_id',$v)->field('a.*,b.price,b.type,b.stock')->select();
            foreach($res as $k1 => $v1){        //循环原料库存信息
                if(isset($data[$v1['m_id']])){
                    $data[$v1['m_id']]['del_num'] = $data[$v1['m_id']]['del_num'] + $post['num'][$k]*$v1['p_use_kg']*$v1['price'];
                    $data[$v1['m_id']]['price'] = $data[$v1['m_id']]['price'] + $post['num'][$k]*$v1['p_use_kg']*$v1['price'];
                    $chengben_price += $post['num'][$k]*$v1['p_use_kg'];
                }else{
                    $data[$v1['m_id']]['order_id'] = $order_id;
                    $data[$v1['m_id']]['store_id'] = 0;
                    $data[$v1['m_id']]['material_id'] = $v1['m_id'];
                    $data[$v1['m_id']]['material_type'] = $v1['type'];
                    $data[$v1['m_id']]['sign'] = 1;
                    $data[$v1['m_id']]['type'] = 1;
                    $data[$v1['m_id']]['del_num'] = $post['num'][$k]*$v1['p_use_kg'];
                    $data[$v1['m_id']]['addtime'] = time();
                    $data[$v1['m_id']]['price'] = $post['num'][$k]*$v1['price']*$v1['p_use_kg'];
                    $data[$v1['m_id']]['signal_price'] = $v1['price'];
                    if($type == 2){         //订货
                        $data[$v1['m_id']]['sign'] = 2;
                        $data[$v1['m_id']]['type'] = 3;
                        $data[$v1['m_id']]['receive_time'] = time();
                        $data[$v1['m_id']]['res_num'] = $post['num'][$k]*$v1['p_use_kg'];
                        $data[$v1['m_id']]['dif_num'] = 0;
                        $data[$v1['m_id']]['cur_num'] = $v1['stock'] - $post['num'][$k]*$v1['p_use_kg'];    //当前库存
                        $data[$v1['m_id']]['receive_signal_price'] = $v1['price'];
                        $data[$v1['m_id']]['receive_price'] = $post['num'][$k]*$v1['p_use_kg']*$v1['price'];
                        $data[$v1['m_id']]['remark'] = '生产使用'.$post['num'][$k]*$v1['p_use_kg'];
                        db('goods_material')->where('id',$v1['m_id'])->setDec('stock',$post['num'][$k]*$v1['p_use_kg']);        //更新原材料库存
                    }
                    $chengben_price += $post['num'][$k]*$v1['p_use_kg']*$v1['price'];
                }
            }
        }

        return ['data'=>$data,'chengben_price'=>$chengben_price,'data1'=>$data1];

    }


}
